﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GrewalCRM.Entities;
using GrewalCRM.BLL;

public partial class UC_LoginUC : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
      if (IsValidUser)
      {
        ltrWellcomeUserName.Text = SessionUser.UserName;
        mvwLogin.SetActiveView(vwWellcome);
      }
      else
      {
        mvwLogin.SetActiveView(vwLogin);
      }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
      UserCRM user = new UserCRM();
      user.LoginID = txtUserName.Text;
      user.Password = txtPwd.Text;
      UserCRM_BLL userBLL = new UserCRM_BLL();

      user = userBLL.GetValideUser(user);

      if (user != null && user.IsActive)
      {
        SessionUser = user;               
        Response.Redirect("Landing.aspx");
      }      
    }
      
    protected void btnLogOut_Click(object sender, EventArgs e)
    {
      Session.Abandon();
      Response.Redirect("default.aspx");

    }

    public bool IsValidUser
    {
      get { return SessionUser != null; }     
    }

    public UserCRM SessionUser
    {
      get 
      {
        if (Session["USER"] != null)
        {
          return Session["USER"] as UserCRM;
        }
        else
        {
          return null;
        }
      }
      set { Session["USER"] = value; }
    }

}